#include <iostream>
using namespace std;
int result[10001];
int num=0;
int main()
{
	int t;
	while(cin>>t){
		for(int i=0;i<t;i++){
			char order;
			int k;
			cin>>order>>k;
			int sum=0;
			if(order>='A'&&order<='Z'){
				sum=sum+(int)order-(int)'A'+1+k;
				result[num]=sum;
				num++;
				continue;
			}
			else{
				sum=sum-((int)order-(int)'a'+1)+k;
				result[num]=sum;
				num++;
				
			}
			
		}
		for(int j=0;j<t;j++){
			cout<<result[j]<<endl;
		}
		num=0;
	}
}
